package com.tcs.tsrm.birch;

import java.io.IOException;
import java.util.ArrayList;

import com.tcs.tsrm.beans.Cluster;
import com.tcs.tsrm.beans.ClusteringThresholds;
import com.tcs.utils.PrintingClusters;

public class BirchClustering {
	private ArrayList<double[]> nonTrivialMatrix;
	private double coinRadius;
	private long preProcessingTime;

	public BirchClustering(ArrayList<double[]> nonTrivialMatrix2,
			double coinRadius2, long preProcessingTime2) {
		this.nonTrivialMatrix = nonTrivialMatrix2;
		this.coinRadius = coinRadius2;
		this.preProcessingTime = preProcessingTime2;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	/**
	 * @param thresholds
	 * @return
	 * @throws IOException
	 */
	public ArrayList<ArrayList<Integer>> executeClustering(
			ClusteringThresholds thresholds) throws IOException {
		int nonTrivialMatSize = nonTrivialMatrix.size();
		long clusteringStart = System.currentTimeMillis();
		JBirch jbirch = new JBirch();
		ArrayList<ArrayList<Integer>> birchClusters = jbirch.executeJBirch(
				nonTrivialMatrix, coinRadius, thresholds.getB());
		long clusteringStop = System.currentTimeMillis();
		long clusteringTime = clusteringStop - clusteringStart;
		System.out.println("Time taken for clustering is ->"+clusteringTime/1000 + " seconds");
		PrintingClusters pc = new PrintingClusters(thresholds.getAlgorithm(),
				thresholds.getMotifWidth(), thresholds.getSensorName(),
				thresholds.isRandomization());
		if (thresholds.isPrintClusteringStats()) {
			pc.printStatsBirch(thresholds.getSensorName(),
					thresholds.getMotifWidth(), thresholds.getAlgorithm(),
					preProcessingTime, nonTrivialMatSize, clusteringTime,
					birchClusters, thresholds.isRandomization(),
					thresholds.isRemoveSingletons(),
					thresholds.isPrintSingletons(), thresholds.getB());

		}
		if (thresholds.isPrintClusters()) {
			pc.printClusterJBirch(birchClusters, thresholds);
		}

		if (thresholds.isPrintOrgSubsClusters()) {
			pc.printBirchClusterOrgSubs(thresholds, birchClusters,
					nonTrivialMatrix);
		}
		if (thresholds.isStatsLSH() && !thresholds.isLshSingletonRemoval()) {
			pc.printBirchSingletonsIndex(birchClusters);
		}
		if (thresholds.isPrintSingletons()) {
			pc.printBirchSingletonsIndex(birchClusters);
		}
		if (thresholds.isPrintOriginalMatrix()) {
			pc.printOriginalMatrix(thresholds, Cluster.originalMatrix);
		}
		if (thresholds.isPrintNonTrivialMatrix()) {
			pc.printNonTrivialMatrix(thresholds, Cluster.nonTrivialMatrix);
		}
		if (thresholds.isPrintCentroids()) {
			pc.printCentroidsBirch(birchClusters, thresholds, nonTrivialMatrix,
					JBirch.originalToNonTrivial);
		}
		if (thresholds.isPrintClustersRedDim()) {
			pc.printBirchClusterRedSubs(thresholds, birchClusters,
					nonTrivialMatrix);
		}

		return birchClusters;
	}
}
